package com.ruoyi.mobile.mapper;

import java.util.List;

import com.baomidou.dynamic.datasource.annotation.DS;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.ruoyi.mobile.domain.SysUserEntity;
import com.ruoyi.system.api.resp.SysUserResp;
import org.apache.ibatis.annotations.*;

/**
 * 用户表 数据层
 */
@Mapper
@DS("master")
public interface ISysUserMapper extends BaseMapper<SysUserEntity> {
    @Select("select a.user_id from sys_user a " +
            "left join sys_user_post b on b.user_id=a.user_id " +
            "left join sys_post c on c.post_id=b.post_id " +
            "where c.post_code not in ('user') " +
            "and a.user_id=#{userId}")
    public SysUserResp getUserInfo(@Param("userId")Long userId);

    @Select("select post_id from sys_post where post_code='user'")
    public SysUserResp getStudentPostInfo();

    @Select("select role_id from sys_role where role_key='common'")
    public SysUserResp getStudentRoleInfo();

    @Select("select post_id from sys_user_post where user_id=#{userId}")
    public SysUserResp getUserPostInfo(@Param("userId")Long userId);

    @Insert("insert into sys_user_post(user_id,post_id) values(#{userId}, #{postId})")
    int insertUserPost(@Param("userId")Long userId, @Param("postId")Long postId);

    @Insert("insert into sys_user_role(user_id,role_id) values(#{userId}, #{roleId})")
    int insertUserRole(@Param("userId")Long userId, @Param("roleId")Long roleId);

    @Update("update sys_user set open_id=#{openId},app_idm=#{appId} where phonenumber=#{phonenumber}")
    int updateUserOpenIdByPhone(@Param("phonenumber")String phonenumber, @Param("openId")String openId, @Param("appId")String appId);

    @Update("update sys_user set hopen_id=#{openId} where phonenumber=#{phonenumber}")
    int updateUserHOpenIdByPhone(@Param("phonenumber")String phonenumber, @Param("openId")String openId);

    @Select("select user_id, dept_id from sys_user where dept_id = #{deptId} and del_flag='0' " +
            "and user_id in (select user_id from sys_user_post where post_id in (select post_id from sys_post where post_code=#{postCode}))")
    public List<SysUserResp> selectUserList(@Param("deptId")Long deptId, @Param("postCode")String postCode);

    @Select("select user_id, dept_id, user_name, nick_name, email, avatar, phonenumber, sex, status, del_flag, remark, app_id, app_idf, app_idm " +
            "from sys_user where open_id=#{openId}")
    public SysUserResp getUserByOpenId(@Param("openId")String openId);

    @Select("select user_id, dept_id, user_name, nick_name, email, avatar, phonenumber, sex, status, del_flag, remark, password " +
            "from sys_user where phonenumber=#{phonenumber}")
    public SysUserResp getUserByPhone(@Param("phonenumber")String phonenumber);

    @Update("update sys_user set password=#{password} where user_id=#{userId}")
    int updateUserPassByUserId(@Param("password")String password, @Param("userId")Long userId);
}
